package net.lab1024.sa.admin.module.business.outbound.service;

import java.util.List;
import net.lab1024.sa.admin.module.business.outbound.dao.OutboundItemDao;
import net.lab1024.sa.admin.module.business.outbound.domain.entity.OutboundItemEntity;
import net.lab1024.sa.admin.module.business.outbound.domain.form.OutboundItemAddForm;
import net.lab1024.sa.admin.module.business.outbound.domain.form.OutboundItemQueryForm;
import net.lab1024.sa.admin.module.business.outbound.domain.form.OutboundItemUpdateForm;
import net.lab1024.sa.admin.module.business.outbound.domain.vo.OutboundItemVO;
import net.lab1024.sa.base.common.util.SmartBeanUtil;
import net.lab1024.sa.base.common.util.SmartPageUtil;
import net.lab1024.sa.base.common.domain.ResponseDTO;
import net.lab1024.sa.base.common.domain.PageResult;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;

import jakarta.annotation.Resource;

/**
 * 出库单明细 Service
 *
 * @Author zzx
 * @Date 2025-08-13 15:21:31
 * @Copyright zzx
 */

@Service
public class OutboundItemService {

    @Resource
    private OutboundItemDao outboundItemDao;

    /**
     * 分页查询
     */
    public PageResult<OutboundItemVO> queryPage(OutboundItemQueryForm queryForm) {
        Page<?> page = SmartPageUtil.convert2PageQuery(queryForm);
        List<OutboundItemVO> list = outboundItemDao.queryPage(page, queryForm);
        return SmartPageUtil.convert2PageResult(page, list);
    }

    /**
     * 添加
     */
    public ResponseDTO<String> add(OutboundItemAddForm addForm) {
        OutboundItemEntity outboundItemEntity = SmartBeanUtil.copy(addForm, OutboundItemEntity.class);
        outboundItemDao.insert(outboundItemEntity);
        return ResponseDTO.ok();
    }

    /**
     * 更新
     *
     */
    public ResponseDTO<String> update(OutboundItemUpdateForm updateForm) {
        OutboundItemEntity outboundItemEntity = SmartBeanUtil.copy(updateForm, OutboundItemEntity.class);
        outboundItemDao.updateById(outboundItemEntity);
        return ResponseDTO.ok();
    }

    /**
     * 批量删除
     */
    public ResponseDTO<String> batchDelete(List<Long> idList) {
        if (CollectionUtils.isEmpty(idList)){
            return ResponseDTO.ok();
        }

        outboundItemDao.batchUpdateDeleted(idList, true);
        return ResponseDTO.ok();
    }

    /**
     * 单个删除
     */
    public ResponseDTO<String> delete(Long outboundItemId) {
        if (null == outboundItemId){
            return ResponseDTO.ok();
        }

        outboundItemDao.updateDeleted(outboundItemId, true);
        return ResponseDTO.ok();
    }
}
